In [ ]:
Numpy----------mathematical calculations
Pandas-----------data manipulation
Matplotlib--------data vizualization
Seaborn--------statistical data vizualization
In [ ]:
plotly:
it is used for vizualizing the data in a interactive way where we can built:
dashboards
web based vizuzlizations
3d plots
effective charts/graphs
In [ ]:
with the help of plotly library we can
Zoom
click
pan
build dashboards for ready applications
In [ ]:
we can easily integrate the plotly library with different libraries like numpy and pandas
In [ ]:
pip install plotly
In [ ]:
import plotly.express as px
In [ ]:
plotly consists of different plots like:
line plot
scatter plot
histogram
bar chart
heat map
box plot
pie chart
area plot
3d vizualization
In [ ]:
1.line plot:
line plot is used to show the trends in the dataset
mainly used in:
time series analysis
trends in the dataset
stock prices
growth analysis
In [1]:
import numpy as np
import plotly.express as px
x=np.arange(1,10,0.5)
y=np.sin(x)
fig=px.line(x=x,y=y,title="Sine wave line plot")
fig.show()
C:\Users\pc\New folder\python\Lib\site-packages\kaleido\_sync_server.py:11: UserWarning: Warning: You have Plotly version 5.24.1, which is not compatible with this version of Kaleido (1.2.0). This means that static image generation (e.g. `fig.write_image()`) will not work. Please upgrade Plotly to version 6.1.1 or greater, or downgrade Kaleido to version 0.2.1.
In [5]:
np.sin(1)
Out[5]:
0.8414709848078965
In [ ]:
2.scatter plot:
it is used to show the relationships between the variables
In [7]:
import numpy as np
import plotly.express as px
x=np.random.randn(100)
y=np.random.randn(100)
fig=px.scatter(x=x,y=y,title="Random scatter plot")
fig.show()
In [ ]:
3.Histograms:
it is used for the data distribution analysis
In [11]:
data=np.random.normal(25,5,500)
fig=px.histogram(data,title="Normal data distribution analysis")
fig.show()
In [ ]:
4.Bar chart:
it is used for comparing the categorical data
sales by region
count analysis
In [13]:
Transport=["Bus","Car","Bike","Auto"]
values=np.random.randint(50,100,4)
fig=px.bar(x=Transport,y=values,title="Count of transportation")
fig.show()
In [ ]:
5.Heat map:
it is used for the correlation analysis
where it shows the matrix form
In [15]:
data=np.random.rand(10,10)
fig=px.imshow(data,title="Heatmap analysis")
fig.show()
In [ ]:
6.Box plot:
it is used for the outlier detection
In [17]:
data=np.random.normal(100,20,200)
fig=px.box(data,title="Box plot Analysis")
fig.show()
In [ ]:
7.pie chart
it is used to show the percentage distribution
In [19]:
labels=["Laptops","Mobiles","Tablet"]
values=np.random.randint(10,50,3)
fig=px.pie(names=labels,values=values,title="Share on products")
fig.show()
In [ ]:
8.Area plot:
used for cummulative analysis
used to show the time series growth analysis
In [21]:
x=np.arange(0,10)
y=np.random.randint(1,20,10)
px.area(x=x,y=y,title="Area plot")
In [ ]:
plotly mainly consists of two interfaces:
1.plotly.express(px)
very simple
one-line chart
2.graph objects(go)
we can do more customization
mainly used in dashboards
In [23]:
import plotly.graph_objects as go
In [25]:
x=np.arange(5)
y=np.random.randint(1,10,5)
In [29]:
go.Figure(data=go.Bar(x=x,y=y))
In [ ]:
complete sales dashboard using plotly
monthly sales trend----line plot
sales by region----bar
correlation analysis---heatmap
profit distribution---histograms
Numpy(data generation)
plotly(vizualization)
subplots(building dashboard style)
In [33]:
import numpy as np
import pandas as pd
np.random.seed(42)
n=500
data=pd.DataFrame({
"Month":np.random.choice(
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],n),
"Region":np.random.choice(
["North","South","East","West"],n),
"Category":np.random.choice(
["Electronics","Clothing","Furniture"],n),
"Sales":np.random.randint(1000,10000,n),
"Profit":np.random.randint(200,3000,n),
"Quantity":np.random.randint(1,10,n)
})
In [35]:
data
Out[35]:
| Month | Region | Category | Sales | Profit | Quantity | |
|---|---|---|---|---|---|---|
| 0 | Jul | South | Furniture | 2409 | 2051 | 5 |
| 1 | Apr | South | Furniture | 5566 | 580 | 6 |
| 2 | Nov | South | Furniture | 5491 | 1846 | 6 |
| 3 | Aug | West | Electronics | 2555 | 213 | 5 |
| 4 | May | North | Furniture | 1804 | 703 | 7 |
| ... | ... | ... | ... | ... | ... | ... |
| 495 | Jun | North | Furniture | 1001 | 2845 | 1 |
| 496 | Sep | South | Electronics | 1374 | 709 | 8 |
| 497 | Dec | East | Furniture | 5648 | 1406 | 7 |
| 498 | May | East | Clothing | 3365 | 585 | 7 |
| 499 | Jan | West | Furniture | 2525 | 1797 | 3 |
500 rows × 6 columns
In [37]:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
In [63]:
monthly_sales=data.groupby("Month")["Sales"].sum().reindex(
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"])
region_sales=data.groupby("Region")["Sales"].sum()
category_sales=data.groupby("Category")["Sales"].sum()
corr=data[['Sales','Profit','Quantity']].corr()
fig=make_subplots(
rows=3,cols=2,
specs=[[{"colspan":2},None],
[{"type":"bar"},{"type":"pie"}],
[{"type":"histogram"},{"type":"heatmap"}]],
subplot_titles=(
"Monthly sales trend",
"sales by region",
"sales by category",
"profit distribution",
"correlation heatmap"
))
fig.add_trace(
go.Scatter(x=monthly_sales.index,y=monthly_sales.values,
mode="lines+markers",name="Monthly Sales"),
row=1,col=1)
fig.add_trace(
go.Bar(x=region_sales.index,y=region_sales.values,
name="Region Sales"),
row=2,col=1)
fig.add_trace(
go.Pie(labels=category_sales.index,values=category_sales.values),
row=2,col=2)
fig.add_trace(
go.Histogram(x=data['Profit'],nbinsx=30),
row=3,col=1)
fig.add_trace(
go.Heatmap(
z=corr.values,
x=corr.columns,
y=corr.columns),row=3,col=2)
fig.update_layout(height=800,width=700,title_text="COMPLETE SALES ANALYTICS DASHBOARD",showlegend=False)
fig.show()
In [ ]:
mini projects:
stock price prediction
sales analysis dashboard
In [65]:
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
# -----------------------------
# Generate Synthetic Stock Data
# -----------------------------
days = 200
dates = np.arange(days)
price = np.cumsum(np.random.normal(0, 1, days)) + 100
open_price = price + np.random.normal(0, 1, days)
close_price = price + np.random.normal(0, 1, days)
high_price = np.maximum(open_price, close_price) + np.random.rand(days) * 2
low_price = np.minimum(open_price, close_price) - np.random.rand(days) * 2
volume = np.random.randint(1000, 5000, days)
# -----------------------------
# Moving Averages
# -----------------------------
ma20 = np.convolve(close_price, np.ones(20)/20, mode='same')
ma50 = np.convolve(close_price, np.ones(50)/50, mode='same')
# -----------------------------
# RSI Calculation
# -----------------------------
delta = np.diff(close_price, prepend=close_price[0])
gain = np.where(delta > 0, delta, 0)
loss = np.where(delta < 0, -delta, 0)
avg_gain = np.convolve(gain, np.ones(14)/14, mode='same')
avg_loss = np.convolve(loss, np.ones(14)/14, mode='same')
rs = avg_gain / (avg_loss + 1e-10)
rsi = 100 - (100 / (1 + rs))
# -----------------------------
# MACD Calculation
# -----------------------------
ema12 = np.convolve(close_price, np.ones(12)/12, mode='same')
ema26 = np.convolve(close_price, np.ones(26)/26, mode='same')
macd = ema12 - ema26
signal = np.convolve(macd, np.ones(9)/9, mode='same')
# -----------------------------
# Create Dashboard Layout
# -----------------------------
fig = make_subplots(
rows=4, cols=1,
shared_xaxes=True,
vertical_spacing=0.02,
row_heights=[0.5, 0.15, 0.15, 0.2],
subplot_titles=("Stock Price", "Volume", "RSI", "MACD")
)
# -----------------------------
# Candlestick
# -----------------------------
fig.add_trace(
go.Candlestick(
x=dates,
open=open_price,
high=high_price,
low=low_price,
close=close_price,
name="Candlestick"
),
row=1, col=1
)
# Moving Averages
fig.add_trace(go.Scatter(x=dates, y=ma20, mode='lines', name='MA20'), row=1, col=1)
fig.add_trace(go.Scatter(x=dates, y=ma50, mode='lines', name='MA50'), row=1, col=1)
# -----------------------------
# Volume
# -----------------------------
fig.add_trace(
go.Bar(x=dates, y=volume, name="Volume"),
row=2, col=1
)
# -----------------------------
# RSI
# -----------------------------
fig.add_trace(
go.Scatter(x=dates, y=rsi, mode='lines', name="RSI"),
row=3, col=1
)
# RSI Overbought/Oversold Lines
fig.add_hline(y=70, line_dash="dash", row=3, col=1)
fig.add_hline(y=30, line_dash="dash", row=3, col=1)
# -----------------------------
# MACD
# -----------------------------
fig.add_trace(go.Scatter(x=dates, y=macd, mode='lines', name="MACD"), row=4, col=1)
fig.add_trace(go.Scatter(x=dates, y=signal, mode='lines', name="Signal"), row=4, col=1)
# -----------------------------
# Layout Settings
# -----------------------------
fig.update_layout(
height=1000,
title="📈 Professional Stock Market Analytics Dashboard",
template="plotly_dark",
xaxis_rangeslider_visible=False
)
fig.show()
In [67]:
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
np.random.seed(42)
# -----------------------------
# Synthetic Business Data
# -----------------------------
days = np.arange(1, 101)
sales = np.cumsum(np.random.randint(50, 150, 100))
profit = sales - np.random.randint(1000, 3000, 100)
customers = np.random.randint(20, 100, 100)
# -----------------------------
# Create Subplots Layout
# -----------------------------
fig = make_subplots(
rows=3, cols=2,
specs=[
[{"colspan": 2}, None],
[{"type": "scatter"}, {"type": "bar"}],
[{"type": "pie"}, {"type": "indicator"}]
],
subplot_titles=(
"Sales & Profit Trend",
"Customer Growth",
"Daily Customers",
"Revenue Distribution",
"KPI Performance"
)
)
# -----------------------------
# Main Trend Line (Sales)
# -----------------------------
fig.add_trace(
go.Scatter(
x=days,
y=sales,
mode="lines",
name="Sales"
),
row=1, col=1
)
# Profit Line
fig.add_trace(
go.Scatter(
x=days,
y=profit,
mode="lines",
name="Profit"
),
row=1, col=1
)
# -----------------------------
# Customer Scatter Plot
# -----------------------------
fig.add_trace(
go.Scatter(
x=days,
y=customers,
mode="markers",
name="Customers"
),
row=2, col=1
)
# -----------------------------
# Bar Chart (Monthly Aggregation)
# -----------------------------
monthly_sales = np.array_split(sales, 5)
monthly_sum = [m.sum() for m in monthly_sales]
fig.add_trace(
go.Bar(
x=["M1","M2","M3","M4","M5"],
y=monthly_sum,
name="Monthly Sales"
),
row=2, col=2
)
# -----------------------------
# Pie Chart
# -----------------------------
fig.add_trace(
go.Pie(
labels=["Product A","Product B","Product C"],
values=[40, 35, 25],
hole=0.4
),
row=3, col=1
)
# -----------------------------
# KPI Gauge
# -----------------------------
fig.add_trace(
go.Indicator(
mode="gauge+number+delta",
value=np.mean(profit),
delta={'reference': 5000},
gauge={
'axis': {'range': [0, 15000]},
},
title={'text': "Avg Profit"}
),
row=3, col=2
)
# -----------------------------
# Dropdown Filter
# -----------------------------
fig.update_layout(
updatemenus=[
dict(
buttons=[
dict(label="Show All",
method="update",
args=[{"visible": [True]*6}]),
dict(label="Only Sales",
method="update",
args=[{"visible": [True, False, False, False, False, False]}]),
dict(label="Only Customers",
method="update",
args=[{"visible": [False, False, True, False, False, False]}])
],
direction="down",
showactive=True,
x=0.1,
y=1.15
)
]
)
# -----------------------------
# Range Slider
# -----------------------------
fig.update_xaxes(
rangeslider_visible=True,
row=1, col=1
)
# -----------------------------
# Layout Settings
# -----------------------------
fig.update_layout(
height=850,
width=850,
title="🚀 Industry-Level Interactive Business Dashboard",
template="plotly_dark"
)
fig.show()
In [ ]:
what is plotly?
what are the different interfaces available in the plotly?
difference between plotly express and graph objects?
how do we create subplots in plotly?
how do you export plotly charts?
how to make interactive dashboards?
what is dashboard?
how to create a dashboard?
what is the use of heatmap?
what are the different types of dashboards available?
In [ ]:
projects:
stock price prediction
sales analysis dashboard
uber data dashboard
blinklit related data dashboard